#ifndef CLIENT_VISTA_CONFIGURATION_H_
#define CLIENT_VISTA_CONFIGURATION_H_
#include <iostream>
#include <gtkmm.h>
#include "../Utils/index.h"
#include "../Utils/directoryHandler.h"

class Configuration: public Gtk::Window{
public:
	Configuration():ip_label("Ip server[127.0.1]: "),port_label("Port[33333]: "), rep_label("Repository[$HOME/AU]: "),
	                polling_label("Polling interval[10]: "), accept_button("Accept"), cancel_button("Cancel"),
	                box(true,0){
		set_size_request(400, 400);
		set_title("Configuration");
		set_border_width(10);
        accept_button.signal_clicked().connect(
				sigc::mem_fun(*this, &Configuration::accept_clicked));
        cancel_button.signal_clicked().connect(
        				sigc::mem_fun(*this, &Configuration::cancel_clicked));
        add(box);

        box.pack_start(ip_label);
        box.pack_start(ip_entry);
        box.pack_start(port_label);
        box.pack_start(port_entry);
        box.pack_start(rep_label);
        box.pack_start(rep_entry);
        box.pack_start(polling_label);
        box.pack_start(polling_entry);
        box.pack_start(hbox);

        hbox.pack_start(accept_button);
        hbox.pack_start(cancel_button);
        show_all_children();

	}
	~Configuration(){}

void default_settings(){
	ip_server="127.0.0.1";
	port="33333";
	repository="./";
	polling = "10";
}
void addDefaultVars(Index& cfg){
	//Creo el campo para el path de indice
	std::string indice_path_field = "INDEX_PATH";
	std::string indice_path = "./indice.txt";
	std::ifstream indice(indice_path.c_str(),std::ifstream::in);
	if(not indice.is_open()){
		Index ind(&indice_path);
		ind.SaveToDisk();
	}
	indice.close();

	//Creo el campo para el path del archivo de login
	std::string login_path_field = "LOGIN_PATH";
	std::string login_path = "./login";

	//Agrego los campos al index
	cfg.AddKey(&indice_path_field,&indice_path);
	cfg.AddKey(&login_path_field,&login_path);
}
protected:
void accept_clicked(){

	std::string file_path = "./cfg.properties";
	Index cfg(&file_path);
	std::string ip_server_text="SERVER_IP";
	std::string port_text="PORT";
	std::string repository_text="AU_DIRECTORY";
	std::string polling_text="POLLING_TIME";
    //Si ingreso algo en el campo, se guarda, de lo contrario se guarda el valor por defecto
	if(ip_entry.get_text_length()){
    	ip_server=ip_entry.get_text();
    }
    else{
    	ip_server="127.0.0.1";
    }
    if(port_entry.get_text_length()){
    	port=port_entry.get_text();
    }
    else{
    	port="33333";
    }
    if(rep_entry.get_text_length()){
    	string userFolder(getenv("HOME"));
    	directoryHandler handler(&userFolder);
    	repository=rep_entry.get_text();
    	std::string rest_dir(repository);
    	std::string dir;
    	std::string acum_dir=userFolder+"/";
    	int pos;
    	// Creacion de directorios, si no existen
    	do{
        	pos=rest_dir.find("/");
        	if (pos == -1){
        		handler.createFolder(&rest_dir);
        	}
        	else{
        		dir = rest_dir.substr(0,pos);
        		rest_dir = rest_dir.substr(pos+1,rest_dir.size()-1);
        		acum_dir+=dir;
        		handler.createFolder(&dir);
        		handler.setDirectoryPath(&acum_dir);
        		acum_dir+="/";
        	}
        }while(pos != -1);
    	repository=userFolder+"/"+repository;
    }
    else{
    	std::string userFolder=getenv("HOME");
    	repository ="AU";
    	directoryHandler handler(&userFolder);
    	handler.createFolder(&repository);
    	repository=userFolder+"/"+repository;

    }
    if(polling_entry.get_text_length()){
    	polling= polling_entry.get_text();
    }
    else{
    	polling="10";
    }
    //Agrega los campos al archivo de configuracion
    cfg.AddKey(&ip_server_text,&ip_server);
	cfg.AddKey(&port_text,&port);
	cfg.AddKey(&repository_text,&repository);
	cfg.AddKey(&polling_text,&polling);
	this->addDefaultVars(cfg);

	cfg.SaveToDisk();
	hide();
}

void cancel_clicked(){
	// No guarda ninguna configuracion
	hide();
}
private:
	Gtk::VBox box;
	Gtk::HBox hbox;
    Gtk::Button accept_button, cancel_button;
	Gtk::Entry ip_entry,port_entry,rep_entry,polling_entry;
	Gtk::Label ip_label,port_label,rep_label,polling_label;
	//variables de configuracion
	std::string ip_server;
	std::string port;
	std::string repository;
	std::string polling;


};
#endif /* CLIENT_VISTA_CONFIGURATION_H_ */
